<?php
namespace app\controller;

use think\Request;
use think\facade\Db;
use think\facade\Log;

class Auth
{
    public function login(Request $request)
    {
        $username = $request->post('username');
        $password = $request->post('password');

        Log::info("用户名: $username");
        Log::info("密码: $password");

        // 参数校验
        if (!$username || !$password) {
            return json(['code' => 400, 'msg' => '用户名或密码不能为空']);
        }

        // 查询用户
        $user = Db::table('user')->where('username', $username)->find();

        if (!$user) {
            return json(['code' => 404, 'msg' => '用户不存在']);
        }

        // 校验密码（这里不使用 md5 加密）
        if ($user['password'] !== $password) {
            return json(['code' => 401, 'msg' => '密码错误']);
        }

        // 登录成功
        return json([
            'code' => 200,
            'msg' => '登录成功',
            'user' => [
                'id' => $user['id'],
                'username' => $user['username']
            ]
        ]);
    }
}
